/*   1:    */ package nl.siegmann.epublib;
/*   2:    */ 
/*   3:    */ import java.io.FileOutputStream;
/*   4:    */ import java.io.OutputStream;
/*   5:    */ import java.io.PrintStream;
/*   6:    */ import java.util.ArrayList;
/*   7:    */ import java.util.List;
/*   8:    */ import nl.siegmann.epublib.bookprocessor.CoverpageBookProcessor;
/*   9:    */ import nl.siegmann.epublib.bookprocessor.DefaultBookProcessorPipeline;
/*  10:    */ import nl.siegmann.epublib.bookprocessor.XslBookProcessor;
/*  11:    */ import nl.siegmann.epublib.chm.ChmParser;
/*  12:    */ import nl.siegmann.epublib.domain.Author;
/*  13:    */ import nl.siegmann.epublib.domain.Book;
/*  14:    */ import nl.siegmann.epublib.domain.Identifier;
/*  15:    */ import nl.siegmann.epublib.domain.Metadata;
/*  16:    */ import nl.siegmann.epublib.domain.Resource;
/*  17:    */ import nl.siegmann.epublib.epub.BookProcessor;
/*  18:    */ import nl.siegmann.epublib.epub.BookProcessorPipeline;
/*  19:    */ import nl.siegmann.epublib.epub.EpubReader;
/*  20:    */ import nl.siegmann.epublib.epub.EpubWriter;
/*  21:    */ import nl.siegmann.epublib.fileset.FilesetBookCreator;
/*  22:    */ import nl.siegmann.epublib.util.VFSUtil;
/*  23:    */ import org.apache.commons.lang.StringUtils;
/*  24:    */ import org.apache.commons.vfs.FileContent;
/*  25:    */ import org.apache.commons.vfs.FileObject;
/*  26:    */ import org.apache.commons.vfs.FileSystemException;
/*  27:    */ import org.apache.commons.vfs.FileSystemManager;
/*  28:    */ import org.apache.commons.vfs.VFS;
/*  29:    */ 
/*  30:    */ public class Fileset2Epub
/*  31:    */ {
/*  32:    */   public static void main(String[] args)
/*  33:    */     throws Exception
/*  34:    */   {
/*  35: 30 */     String inputLocation = "";
/*  36: 31 */     String outLocation = "";
/*  37: 32 */     String xslFile = "";
/*  38: 33 */     String coverImage = "";
/*  39: 34 */     String title = "";
/*  40: 35 */     List<String> authorNames = new ArrayList();
/*  41: 36 */     String type = "";
/*  42: 37 */     String isbn = "";
/*  43: 38 */     String inputEncoding = "UTF-8";
/*  44: 39 */     List<String> bookProcessorClassNames = new ArrayList();
/*  45: 41 */     for (int i = 0; i < args.length; i++) {
/*  46: 42 */       if (args[i].equalsIgnoreCase("--in")) {
/*  47: 43 */         inputLocation = args[(++i)];
/*  48: 44 */       } else if (args[i].equalsIgnoreCase("--out")) {
/*  49: 45 */         outLocation = args[(++i)];
/*  50: 46 */       } else if (args[i].equalsIgnoreCase("--input-encoding")) {
/*  51: 47 */         inputEncoding = args[(++i)];
/*  52: 48 */       } else if (args[i].equalsIgnoreCase("--xsl")) {
/*  53: 49 */         xslFile = args[(++i)];
/*  54: 50 */       } else if (args[i].equalsIgnoreCase("--book-processor-class")) {
/*  55: 51 */         bookProcessorClassNames.add(args[(++i)]);
/*  56: 52 */       } else if (args[i].equalsIgnoreCase("--cover-image")) {
/*  57: 53 */         coverImage = args[(++i)];
/*  58: 54 */       } else if (args[i].equalsIgnoreCase("--author")) {
/*  59: 55 */         authorNames.add(args[(++i)]);
/*  60: 56 */       } else if (args[i].equalsIgnoreCase("--title")) {
/*  61: 57 */         title = args[(++i)];
/*  62: 58 */       } else if (args[i].equalsIgnoreCase("--isbn")) {
/*  63: 59 */         isbn = args[(++i)];
/*  64: 60 */       } else if (args[i].equalsIgnoreCase("--type")) {
/*  65: 61 */         type = args[(++i)];
/*  66:    */       }
/*  67:    */     }
/*  68: 64 */     if ((StringUtils.isBlank(inputLocation)) || (StringUtils.isBlank(outLocation))) {
/*  69: 65 */       usage();
/*  70:    */     }
/*  71: 67 */     BookProcessorPipeline epubCleaner = new DefaultBookProcessorPipeline();
/*  72: 68 */     epubCleaner.addBookProcessors(createBookProcessors(bookProcessorClassNames));
/*  73: 69 */     EpubWriter epubWriter = new EpubWriter(epubCleaner);
/*  74: 70 */     if (!StringUtils.isBlank(xslFile)) {
/*  75: 71 */       epubCleaner.addBookProcessor(new XslBookProcessor(xslFile));
/*  76:    */     }
/*  77: 74 */     if (StringUtils.isBlank(inputEncoding)) {
/*  78: 75 */       inputEncoding = "UTF-8";
/*  79:    */     }
/*  80:    */     Book book;
/*  81:    */     Book book;
/*  82: 79 */     if ("chm".equals(type))
/*  83:    */     {
/*  84: 80 */       book = ChmParser.parseChm(VFSUtil.resolveFileObject(inputLocation), inputEncoding);
/*  85:    */     }
/*  86:    */     else
/*  87:    */     {
/*  88:    */       Book book;
/*  89: 81 */       if ("epub".equals(type)) {
/*  90: 82 */         book = new EpubReader().readEpub(VFSUtil.resolveInputStream(inputLocation), inputEncoding);
/*  91:    */       } else {
/*  92: 84 */         book = FilesetBookCreator.createBookFromDirectory(VFSUtil.resolveFileObject(inputLocation), inputEncoding);
/*  93:    */       }
/*  94:    */     }
/*  95: 87 */     if (StringUtils.isNotBlank(coverImage))
/*  96:    */     {
/*  97: 89 */       book.setCoverImage(new Resource(VFSUtil.resolveInputStream(coverImage), coverImage));
/*  98: 90 */       epubCleaner.getBookProcessors().add(new CoverpageBookProcessor());
/*  99:    */     }
/* 100: 93 */     if (StringUtils.isNotBlank(title))
/* 101:    */     {
/* 102: 94 */       List<String> titles = new ArrayList();
/* 103: 95 */       titles.add(title);
/* 104: 96 */       book.getMetadata().setTitles(titles);
/* 105:    */     }
/* 106: 99 */     if (StringUtils.isNotBlank(isbn)) {
/* 107:100 */       book.getMetadata().addIdentifier(new Identifier("ISBN", isbn));
/* 108:    */     }
/* 109:103 */     initAuthors(authorNames, book);
/* 110:    */     OutputStream result;
/* 111:    */     try
/* 112:    */     {
/* 113:107 */       result = VFS.getManager().resolveFile(outLocation).getContent().getOutputStream();
/* 114:    */     }
/* 115:    */     catch (FileSystemException e)
/* 116:    */     {
/* 117:109 */       result = new FileOutputStream(outLocation);
/* 118:    */     }
/* 119:111 */     epubWriter.write(book, result);
/* 120:    */   }
/* 121:    */   
/* 122:    */   private static void initAuthors(List<String> authorNames, Book book)
/* 123:    */   {
/* 124:115 */     if ((authorNames == null) || (authorNames.isEmpty())) {
/* 125:116 */       return;
/* 126:    */     }
/* 127:118 */     List<Author> authorObjects = new ArrayList();
/* 128:119 */     for (String authorName : authorNames)
/* 129:    */     {
/* 130:120 */       String[] authorNameParts = authorName.split(",");
/* 131:121 */       Author authorObject = null;
/* 132:122 */       if (authorNameParts.length > 1) {
/* 133:123 */         authorObject = new Author(authorNameParts[1], authorNameParts[0]);
/* 134:124 */       } else if (authorNameParts.length > 0) {
/* 135:125 */         authorObject = new Author(authorNameParts[0]);
/* 136:    */       }
/* 137:127 */       authorObjects.add(authorObject);
/* 138:    */     }
/* 139:129 */     book.getMetadata().setAuthors(authorObjects);
/* 140:    */   }
/* 141:    */   
/* 142:    */   private static List<BookProcessor> createBookProcessors(List<String> bookProcessorNames)
/* 143:    */   {
/* 144:134 */     List<BookProcessor> result = new ArrayList(bookProcessorNames.size());
/* 145:135 */     for (String bookProcessorName : bookProcessorNames)
/* 146:    */     {
/* 147:136 */       BookProcessor bookProcessor = null;
/* 148:    */       try
/* 149:    */       {
/* 150:138 */         bookProcessor = (BookProcessor)Class.forName(bookProcessorName).newInstance();
/* 151:139 */         result.add(bookProcessor);
/* 152:    */       }
/* 153:    */       catch (Exception e)
/* 154:    */       {
/* 155:141 */         e.printStackTrace();
/* 156:    */       }
/* 157:    */     }
/* 158:144 */     return result;
/* 159:    */   }
/* 160:    */   
/* 161:    */   private static void usage()
/* 162:    */   {
/* 163:148 */     System.out.println("usage: " + Fileset2Epub.class.getName() + "\n  --author [lastname,firstname]" + "\n  --cover-image [image to use as cover]" + "\n  --input-ecoding [text encoding]  # The encoding of the input html files. If funny characters show" + "\n                             # up in the result try 'iso-8859-1', 'windows-1252' or 'utf-8'" + "\n                             # If that doesn't work try to find an appropriate one from" + "\n                             # this list: http://en.wikipedia.org/wiki/Character_encoding" + "\n  --in [input directory]" + "\n  --isbn [isbn number]" + "\n  --out [output epub file]" + "\n  --title [book title]" + "\n  --type [input type, can be 'epub', 'chm' or empty]" + "\n  --xsl [html post processing file]");
/* 164:    */     
/* 165:    */ 
/* 166:    */ 
/* 167:    */ 
/* 168:    */ 
/* 169:    */ 
/* 170:    */ 
/* 171:    */ 
/* 172:    */ 
/* 173:    */ 
/* 174:    */ 
/* 175:    */ 
/* 176:    */ 
/* 177:162 */     System.exit(0);
/* 178:    */   }
/* 179:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.Fileset2Epub
 * JD-Core Version:    0.7.0.1
 */